package com.example.xyxwebbackend001.service.impl;

import cn.dev33.satoken.stp.StpUtil;
import com.example.xyxwebbackend001.mapper.RoleUserMapper;
import com.example.xyxwebbackend001.mapper.RolesxMapper;
import com.example.xyxwebbackend001.mapper.USERMapper;
import com.example.xyxwebbackend001.model.DTO.UserDTO;
import com.example.xyxwebbackend001.model.entity.RoleUser;
import com.example.xyxwebbackend001.model.entity.User;
import com.example.xyxwebbackend001.model.vo.UserInfoVO;
import com.example.xyxwebbackend001.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Optional;

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private USERMapper userMapper;

    @Autowired
    private RolesxMapper rolesxMapper;

    @Autowired
    private RoleUserMapper roleUserMapper;
    @Override
    public UserDTO authUser(String userName, String password) {
        return Optional.ofNullable(userMapper.authUserByNameAndPassword(userName, password))
                .map(user -> {
                    UserDTO dto = new UserDTO();
                    dto.setUserId(user.getUserId());
                    dto.setUsername(user.getUsername());
                    dto.setEmail(user.getEmail());
                    dto.setAvatarPath(user.getAvatarPath());
                    return dto;
                })
                .orElse(null);
    }

    @Override
    public List<String> getRolesByUserId(int userId) {
        return rolesxMapper.getRoles(userId);
    }

    @Override
    public void logout() {
        StpUtil.logout();
    }

    @Override
    public boolean checkUsername(String username) {
        String user = userMapper.getUsername(username);
        if(user != null){
            return true;
        }else {
            return false;
        }
    }

    @Override
    public boolean checkEmailExists(String email) {
        return userMapper.selectCountByEmail(email) > 0;
    }

    @Override
    public Integer createUser(User user) {
        userMapper.insert(user);
        Integer userId = user.getUserId();
        if (userId != null) {
            RoleUser roleUser = new RoleUser();
            roleUser.setUserId(userId);
            roleUser.setRoleId(2);
            roleUserMapper.insert(roleUser);
            return userId;
        }
        return null;
    }

    @Override
    public List<UserInfoVO> getUsersByRole(String role, String keyword) {
        return userMapper.selectUsersByRole(role, keyword);
    }

    @Override
    public int resetPassword(Integer userId) {
        return userMapper.resetPassword(userId, "123456");
    }

    @Override
    public int updateUserStatus(Integer userId, Integer status) {
        return userMapper.updateUserStatus(userId, status);
    }

    @Override
    public int forgetPassword(String email, String newPassword) {
        return userMapper.forgetPassword(email, newPassword);
    }


}
